<?php
abstract class XA_Db {

   public $connection;
   public $transaction_level = 0;
   protected $insert_id;
   public static $instance;



	abstract public function __construct($host, $user, $password, $db_name);
	abstract public function query($query);



	public function getInsertId() {
		return $this->insert_id;
	}



	public static function getInstance() {
		// TODO use dynamic class name dict
		if (!self::$instance) {
			if (defined('DB_TYPE') && DB_TYPE === 'pgsql') {
				self::$instance = new XA_Db_PgSQL(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
			} elseif (defined('DB_TYPE') && DB_TYPE === 'sybase') {
				self::$instance = new XA_Db_Sybase(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
			} else {
				self::$instance = new XA_Db_MySQL(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
			}
		}

		return self::$instance;
	}



   public static function begin() {
      $self->transaction_level++;
      return self::getInstance()->begin_trans();
   }



	public static function end($commit = false) {
		$self->transaction_level--;
		// TODO take transaction level in account
      return self::getInstance()->end_trans($commit);
   }



	public static function esc($string) {
		return self::getInstance()->escapeString($string);
	}
}

